package com.stone.elec.rename.executor.impl;

import java.io.File;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.io.FileUtil;
import com.stone.elec.rename.config.ReNameConfig;
import com.stone.elec.rename.executor.BaseExecutor;
import com.stone.elec.rename.executor.ExecutorFactory;
import com.stone.elec.rename.utils.FileUtils;

public class DbExecutor implements BaseExecutor {

    private String path = "db";

    private ReNameConfig reNameConfig;

    private static DbExecutor dbExecutor = new DbExecutor();

    public DbExecutor() {
        reNameConfig = ReNameConfig.reNameConfig;
    }

    public static void put() {
        ExecutorFactory.put("db", dbExecutor);
    }

    @Override
    public void execute() {
        //修改db
        List<String> fileName = FileUtil.listFileNames(reNameConfig.getBasePath() + "\\" + path);
        //System.out.println("获取文件列表 {}", fileName);

        fileName.stream().forEach(v -> {
            //读取file
            File file = FileUtil.file(reNameConfig.getBasePath() + "\\" + path + "\\" + v);

            //在验证文件内容
            FileUtils.projectFileParse(file);
            //先验证文件名
            fileNameValidate(file);
        });

    }

    private void fileContentValidate(File file) {
        FileUtils.sqlValidate(file);
        //Dockerfile.MAINTAINER,2elecx.sql.pig todo

    }

    private void fileNameValidate(File file) {
        String fileName = file.getName();
        if (fileName.contains(reNameConfig.getOldProjectName())) {

            fileName = fileName.replace(reNameConfig.getOldProjectName(), reNameConfig.getProjectName());

            FileUtil.rename(file, fileName, false, true);
        }
    }
}
